package com.taritari.journal.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * @author taritari
 * @mood happy
 */
@Component
public class RsaUtils {
    /**
     * 公钥
     */
    @Value("${rsa.publicKeyPEM}")
    private  String publicKey;
    /**
     * 私钥
     */
    @Value("${rsa.privateKeyPEM}")
    private  String privateKey;


    /**
     * 编码
     */
    private final String CHARSET = "UTF-8";


    /**
     * 初始化
     */
    public static void init() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        // 设置密钥长度
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey aPublic = keyPair.getPublic();
        PrivateKey aPrivate = keyPair.getPrivate();
        System.out.println(aPublic);
        System.out.println(aPrivate);
    }

    public PublicKey getPublicKeyFromString() throws GeneralSecurityException {

        RsaUtils rsaUtils = new RsaUtils();
        // Base64解码
        byte[] encoded = java.util.Base64.getDecoder().decode(publicKey);
        // 使用X509EncodedKeySpec类创建公钥规范
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
        // 获取KeyFactory实例并生成PublicKey对象
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePublic(keySpec);
    }

    public PrivateKey getPrivateKeyFromString() throws GeneralSecurityException {
        RsaUtils rsaUtils = new RsaUtils();
        // Base64解码
        byte[] encoded = java.util.Base64.getDecoder().decode(privateKey);
        // 使用PKCS8EncodedKeySpec类创建私钥规范
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
        // 获取KeyFactory实例并生成PrivateKey对象
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(keySpec);
    }
    /**
     * 加密
     * */
    public String encrypt(String plainText) throws Exception {
        PublicKey  publicKey = getPublicKeyFromString();
        Cipher encryptCipher = Cipher.getInstance("RSA");
        encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = encryptCipher.doFinal(plainText.getBytes());
        String s = Base64.getEncoder().encodeToString(bytes);
        return s;
    }
    /**
     * 解密
     * */
    public String decrypt(String cipherText) throws Exception {
        PrivateKey privateKey = getPrivateKeyFromString();
        Cipher decryptCipher = Cipher.getInstance("RSA");
        decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
        // 将Base64编码的字符串解码为原始字节数据
        byte[] decodedBytes = Base64.getDecoder().decode(cipherText);
        // 解密字节数据
        byte[] decryptedBytes = decryptCipher.doFinal(decodedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

}